//修改基金定投：/change-auto-buy-position
import http from 'k6/http';
import { check } from 'k6';
import { LOGIN, URL } from './config.js';

// 1. 压测场景：10 个虚拟用户，持续 30 秒
export const options = {
  vus: 10,
  duration: '30s',
};

// 2. 常量
//const BASE = 'https://dev.xiaobeiyangji.com/yangji-api';
const BASE = `${URL}/yangji-api`;

export default function () {
  // 登录 → 拿 accessToken: https://dev.xiaobeiyangji.com/api/login/phone
 const loginRes = http.post(`${BASE}/api/login/phone`,JSON.stringify({
       phone: '15298055260',
       code: '666666',
       clientType: 'PHONE'
     }),
     { headers: { 'Content-Type': 'application/json' } }
   );
  //  https://api.xiaobeiyangji.com/api/change-auto-buy-position
  const payload=JSON.stringify(
{
    "_id":"0da653eb-1aef-443f-bab4-23e820afa4bd",
    "accountId":"9f57744b-cd04-4425-8ec2-203dd4e8bc53",
    "buyMoney":100,
    "code":"519674",
    "fees":0.15,
    "nextDate":"10月27日",
    "period":"everyWeek",
    "periodChilder":"everyMon",
    "periodName":"每周周一",
    "rates":0.15,
    "state":"normal",
    "tcbContext":{},
    "userInfo":{"appId":"wx91edeb1dcb0d1011","openId":"oWO3a5AU5yjbVrpI87H8wHsTzDf4"},
    "version":"3.4.0.X",
    "unionId":"o896o5_scUqyJKkRTR1z9Wbm4V98",
    "_createTime":"2025-10-24 20:17:06",
    "_updateTime":"2025-10-24 20:22:00",
    "updateDate":"20251024",
    "nextAutoDate":"2025-10-27"}
  );

  // 简单断言
  check(loginRes, { 'login 200': r => r.status === 200 });

  // 提取 token（返回体在 data.accessToken）
  const token = loginRes.json('data.accessToken');

  // ② 用 token 调 /api/get-version
  const verRes = http.post(`${BASE}/api/change-auto-buy-position`,payload,{ headers: {'Content-Type': 'application/json', Authorization: `Bearer ${token}` } }
  );
  console.log('状态码',verRes.status);
  console.log('返回值',verRes.body);
}